package com.example.home.rubbish;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.home.R;
import com.example.home.rubbish.activity.NewsClassifyActivity;
import com.example.home.rubbish.activity.NewsSearchActivity;
import com.example.home.rubbish.adapter.RubbishBannerAdapter;
import com.example.home.rubbish.adapter.RubbishNewsAdapter;
import com.example.home.rubbish.bean.RubbishBannerBean;
import com.example.home.rubbish.bean.RubbishNewsBean;
import com.example.home.rubbish.bean.TabBean;
import com.example.home.tool.OKHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.Timer;
import java.util.TimerTask;

public class RubbishMainActivity extends AppCompatActivity {
    private ImageView back;
    private ViewPager vpBanner;
    private ImageView btnSearch;
    private ImageView btnClassify;
    private TabLayout tabList;
    private RecyclerView rcvList;
    private Timer timer;
    private int currentPage = 0;
    private final long DELAY_MS = 1000;
    private final long PERIOD_MS = 3000;
    private RubbishBannerAdapter rubbishBannerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rubbish_main);
        initView();

        //轮播图
        getBanner();
        startAutoScroll();

        //分类
        getTabData();
        NewsListData(7);
    }

    private void getTabData() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/news-type/list",
                OKHttpTool.GET,
                TabBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        TabBean tabBean = (TabBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                for (TabBean.RowsBean rowsBean : tabBean.getRows()){
                                    TabLayout.Tab tab = tabList.newTab();
                                    tab.setText(rowsBean.getName());
                                    tabList.addTab(tab);
                                    newList(rowsBean.getId());
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void newList(Integer id) {
        tabList.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String NewsName =tab.getText().toString();
                if (NewsName.equals("新时尚")){
                    NewsListData(id);
                }else if (NewsName.equals("党员在行动")){
                    NewsListData(id);
                }else if (NewsName.equals("分类达人")){
                    NewsListData(id);
                }else if (NewsName.equals("社区动态")){
                    NewsListData(id);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void NewsListData(Integer id) {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/news/list?type="+id,
                OKHttpTool.GET,
                RubbishNewsBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RubbishNewsBean rubbishBannerBean = (RubbishNewsBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindNewsList(rubbishBannerBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindNewsList(RubbishNewsBean rubbishBannerBean) {
        RecyclerView recyclerView = findViewById(R.id.rcv_list);
        RubbishNewsAdapter rubbishNewsAdapter = new RubbishNewsAdapter(RubbishMainActivity.this,rubbishBannerBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(RubbishMainActivity.this);
        recyclerView.setAdapter(rubbishNewsAdapter);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    private void getBanner() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/ad-banner/list",
                OKHttpTool.GET,
                RubbishBannerBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RubbishBannerBean rubbishBannerBean = (RubbishBannerBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindBannershow(rubbishBannerBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindBannershow(RubbishBannerBean rubbishBannerBean) {
        ViewPager viewPager = findViewById(R.id.vp_banner);
        rubbishBannerAdapter = new RubbishBannerAdapter(RubbishMainActivity.this, rubbishBannerBean.getData());
        viewPager.setAdapter(rubbishBannerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void startAutoScroll() {
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            @Override
            public void run() {
                if (currentPage == rubbishBannerAdapter.getCount()) {
                    currentPage = 0;
                }
                vpBanner.setCurrentItem(currentPage++, true);
                ;
            }
        };
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(update);
            }
        }, DELAY_MS, PERIOD_MS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }


    private void initView() {

        back = findViewById(R.id.back);
        vpBanner = findViewById(R.id.vp_banner);
        btnSearch = findViewById(R.id.btn_search);
        btnClassify = findViewById(R.id.btn_classify);
        tabList = findViewById(R.id.tab_list);
        rcvList = findViewById(R.id.rcv_list);


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RubbishMainActivity.this.finish();
            }
        });

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RubbishMainActivity.this, NewsSearchActivity.class);
                startActivity(intent);
            }
        });
        btnClassify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RubbishMainActivity.this, NewsClassifyActivity.class);
                startActivity(intent);
            }
        });

    }
}